<!--
**PageHeader 组件使用指南**

这是一个可高度自定义的页面头部组件，适用于各类内容页面，可以实现与导航栏融合的美观效果。

## 基本用法

```vue
<PageHeader 
  pageTitle="文章归档" 
  :showSubtitle="true" 
  height="45vh" 
/>
```

## 所有可配置属性

| 属性名                  | 类型           | 默认值                   | 描述                                 |
|------------------------|---------------|--------------------------|--------------------------------------|
| pageTitle              | String        | "KINGCOLA-ICG BLOG"      | 页面标题文本                          |
| height                 | String        | "50vh"                   | 组件高度                              |
| showSubtitle           | Boolean       | true                     | 是否显示带打字效果的副标题              |
| staticSubtitle         | String        | ""                       | 静态副标题（当showSubtitle=false时显示）|
| customTexts            | Array         | null                     | 自定义打字文本数组                     |
| titleFontSize          | String        | "3rem"                   | 标题字体大小                          |
| showWaves              | Boolean       | true                     | 是否显示底部波浪效果                   |
| showBackgroundSwitch   | Boolean       | true                     | 是否显示背景切换按钮                   |
| defaultBackgroundImage | String        | "https://images.pexels..." | 默认背景图片URL                     |
| autoChangeBackground   | Boolean       | true                     | 是否自动切换背景图                     |
| backgroundChangeInterval| Number       | 60000                    | 自动切换背景的时间间隔(ms)             |
| imageSourcesConfig     | Array         | ['fj','ysz','ycy',...]   | 图片API请求源数组                     |
| showArrowDown          | Boolean       | true                     | 是否显示向下滚动的箭头                 |

## 使用示例

1. **标准配置**
```vue
<PageHeader 
  pageTitle="文章列表" 
  height="45vh"
/>
```

2. **静态副标题（不带打字效果）**
```vue
<PageHeader 
  pageTitle="关于我们" 
  height="40vh"
  :showSubtitle="false"
  staticSubtitle="技术驱动创新，创新改变生活"
/>
```

3. **自定义打字文本**
```vue
<PageHeader 
  pageTitle="技术博客" 
  :customTexts="['深度技术分享','前沿科技动态','编程最佳实践']"
/>
```

4. **简洁模式（无波浪和背景切换）**
```vue
<PageHeader 
  pageTitle="联系我们" 
  height="30vh"
  :showWaves="false"
  :showBackgroundSwitch="false"
/>
```

5. **完全自定义**
```vue
<PageHeader 
  pageTitle="项目展示" 
  titleFontSize="3.5rem"
  height="60vh"
  :showWaves="true"
  :customTexts="['创意无限','技术卓越','用户至上']"
  defaultBackgroundImage="/path/to/custom/image.jpg"
  :backgroundChangeInterval="30000"
/>
```

6. **自定义图片源**
```vue
<PageHeader 
  pageTitle="精选图库" 
  :imageSourcesConfig="['moez', 'ai']"
/>
```

7. **隐藏滚动箭头**
```vue
<PageHeader 
  pageTitle="简洁头部" 
  :showArrowDown="false"
/>
```

## 注意事项

- 组件默认与导航栏融合，如需调整，可修改margin-top和padding-top值
- 默认图片API来源包括：'fj', 'ysz', 'ycy', 'moez', 'ai', 'moe'
- 在移动设备上组件会自动调整为较小的高度和字体大小
-->

<template>
    <div ref="headerRef" class="page-header-container" :class="{ 'is-loading': isImageLoading }"
        :style="{ backgroundImage: backgroundImage ? `url(${backgroundImage})` : 'none', height: height }">
        <div class="header-mask"></div>
        <div class="header-content">
            <h1 class="page-title" :style="{ fontSize: titleFontSize }">{{ pageTitle }}</h1>
            <div class="subtitle-container" v-if="showSubtitle">
                <div class="subtitle">
                    <span class="typed-text">{{ typedText }}</span>
                    <span class="easy-typed-cursor">|</span>
                </div>
            </div>
            <div class="static-subtitle" v-if="staticSubtitle && !showSubtitle">
                {{ staticSubtitle }}
            </div>
        </div>

        <div class="switch-background-btn" v-if="showBackgroundSwitch" @click="handleSwitchBackground">
            <i class="fas fa-random"></i>
            <span class="hover-tip">切换背景</span>
        </div>

        <Waves v-if="showWaves" class="hero-waves" />
        <i v-if="showArrowDown" class="fas fa-chevron-down arrow-down" @click="scrollDown"></i>
    </div>
</template>

<script>
import axios from "axios";
import Waves from "@/components/Waves/index.vue";

export default {
    name: "PageHeader",
    components: {
        Waves,
    },
    props: {
        pageTitle: {
            type: String,
            default: "KINGCOLA-ICG BLOG"
        },
        showSubtitle: {
            type: Boolean,
            default: true
        },
        height: {
            type: String,
            default: "50vh"
        },
        customTexts: {
            type: Array,
            default: null
        },
        staticSubtitle: {
            type: String,
            default: ""
        },
        showWaves: {
            type: Boolean,
            default: true
        },
        showBackgroundSwitch: {
            type: Boolean,
            default: true
        },
        titleFontSize: {
            type: String,
            default: "3rem"
        },
        defaultBackgroundImage: {
            type: String,
            default: "https://images.pexels.com/photos/2387793/pexels-photo-2387793.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"
        },
        autoChangeBackground: {
            type: Boolean,
            default: true
        },
        backgroundChangeInterval: {
            type: Number,
            default: 60000 // 默认60秒
        },
        imageSourcesConfig: {
            type: Array,
            default: () => ['fj', 'ysz', 'ycy', 'moez', 'ai', 'moe']
        },
        showArrowDown: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            backgroundImage: this.defaultBackgroundImage,
            isImageLoading: false,
            typedText: "",
            textsToType: [
                "始于代码，不止于代码。",
                "代码，是创造的起点。",
                "每一行，都是热爱。",
                "探索技术，无止境。",
                "分享知识，共成长。",
                "从零到一，见证改变。",
                "保持好奇，迎接变化。",
                "技术塑未来，思想引方向。"
            ],
            currentTextToType: "",
            typingSpeed: 120,
            typingInterval: null,
            imageInterval: null,
            charIndex: 0,
            deleting: false,
            delayCounter: 0
        };
    },
    computed: {
        activeTexts() {
            return this.customTexts || this.textsToType;
        }
    },
    methods: {
        handleSwitchBackground() {
            this.fetchImage();
        },
        fetchImage() {
            this.isImageLoading = true;

            const loadImage = (url) => {
                const img = new Image();
                img.src = url;
                img.onload = () => {
                    setTimeout(() => {
                        this.backgroundImage = url;
                        this.isImageLoading = false;
                    }, 300);
                };
                img.onerror = () => {
                    this.isImageLoading = false;
                };
            };

            const imageSources = this.imageSourcesConfig;
            const randomSource = imageSources[Math.floor(Math.random() * imageSources.length)];
            const apiUrl = `${import.meta.env.VITE_APP_BASE_API}/api/image/${randomSource}`;

            axios
                .get(apiUrl)
                .then((response) => {
                    let imageUrl = null;
                    if (response.data && response.data.code === 200 && response.data.data) {
                        if (typeof response.data.data === 'string' && response.data.data.startsWith('http')) {
                            imageUrl = response.data.data;
                        }
                    }

                    if (imageUrl) {
                        loadImage(imageUrl);
                    } else {
                        this.isImageLoading = false;
                    }
                })
                .catch(() => {
                    this.isImageLoading = false;
                });
        },
        startTyping() {
            if (this.deleting) {
                if (this.typedText.length > 0) {
                    this.typedText = this.typedText.substring(0, this.typedText.length - 1);
                    this.typingInterval = setTimeout(this.startTyping, 50);
                } else {
                    this.deleting = false;
                    this.selectNewText();
                    this.typingInterval = setTimeout(this.startTyping, 500);
                }
            } else {
                if (this.charIndex < this.currentTextToType.length) {
                    this.typedText += this.currentTextToType.charAt(this.charIndex);
                    this.charIndex++;
                    const randomSpeed = Math.floor(Math.random() * 80) + this.typingSpeed;
                    this.typingInterval = setTimeout(this.startTyping, randomSpeed);
                } else {
                    if (this.delayCounter < 20) {
                        this.delayCounter++;
                        this.typingInterval = setTimeout(this.startTyping, 100);
                    } else {
                        this.delayCounter = 0;
                        this.deleting = true;
                        this.typingInterval = setTimeout(this.startTyping, 1000);
                    }
                }
            }
        },
        selectNewText() {
            const randomIndex = Math.floor(Math.random() * this.activeTexts.length);
            this.currentTextToType = this.activeTexts[randomIndex];
            this.charIndex = 0;
        },
        resetAndStartTyping() {
            clearTimeout(this.typingInterval);
            this.typedText = "";
            this.charIndex = 0;
            this.deleting = false;
            this.delayCounter = 0;
            this.selectNewText();
            this.startTyping();
        },
        scrollDown() {
            const contentElement = this.$el.nextElementSibling;
            if (contentElement) {
                contentElement.scrollIntoView({ behavior: 'smooth' });
            }
        }
    },
    mounted() {
        this.fetchImage();
        if (this.showSubtitle) {
            this.resetAndStartTyping();
        }
        if (this.autoChangeBackground) {
            this.imageInterval = setInterval(this.fetchImage, this.backgroundChangeInterval);
        }
    },
    beforeDestroy() {
        clearTimeout(this.typingInterval);
        clearInterval(this.imageInterval);
    },
};
</script>

<style lang="scss" scoped>
.page-header-container {
    position: relative;
    color: #fff;
    background-position: center top;
    background-size: cover;
    transition: background-image 1.5s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    overflow: hidden;
    z-index: 1;
    margin-top: -70px;
    /* 与导航栏融合 */
    padding-top: 60px;
    /* 补偿导航栏的高度 */

    &:not(.is-loading) {
        background-size: 110%;
        animation: subtle-zoom 35s ease-in-out infinite alternate;
    }
}

.page-header-container.is-loading {
    background: linear-gradient(-60deg,
            #0b0217,
            #0e0524,
            #1d0c3d,
            #3c165a,
            #1d0c3d,
            #0e0524,
            #0b0217);
    background-size: 300% 300%;
    animation: gradient-flow 8s ease-in-out infinite;
}

@keyframes gradient-flow {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

@keyframes subtle-zoom {
    0% {
        background-size: 100%;
        background-position: 50% 0%;
    }

    100% {
        background-size: 110%;
        background-position: 30% 10%;
    }
}

.header-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom,
            rgba(0, 0, 0, 0.5) 0%,
            rgba(0, 0, 0, 0.4) 40%,
            rgba(0, 0, 0, 0.6) 100%);
    z-index: 0;
}

.header-content {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 0 20px;
    width: auto;
    margin-top: 20px;
}

.page-title {
    font-family: "Freestyle Script", "Mistral", "fantasy";
    line-height: 1.2;
    margin-bottom: 1.5rem;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.6),
        0 4px 6px rgba(0, 0, 0, 0.4);
    font-weight: 700;
    letter-spacing: 1.5px;
    position: relative;
    background: linear-gradient(to right, #ffffff, #e6e6ff, #c9d9ff, #e6e6ff, #ffffff);
    background-size: 200% 100%;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: shimmer 8s infinite linear;
    padding: 0 15px;
    max-width: 100%;
    overflow: visible;
    transform: scale(0.95);
    transition: all 0.3s ease;
    cursor: default;
}

.page-title:hover {
    transform: scale(1);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.7),
        0 6px 12px rgba(0, 0, 0, 0.5);
    letter-spacing: 2px;
}

@keyframes shimmer {
    0% {
        background-position: 0% 0;
    }

    100% {
        background-position: 200% 0;
    }
}

.subtitle-container {
    max-width: 90%;
    margin: 0 auto;
    position: relative;
    overflow: hidden;
    padding: 10px 5px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.static-subtitle {
    font-size: 1.5rem;
    font-weight: 500;
    margin-top: 1rem;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
    max-width: 90%;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    padding: 10px 20px;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.subtitle {
    font-size: 1.5rem;
    font-weight: 500;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
    white-space: nowrap;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 2.2rem;
}

.typed-text {
    display: inline-block;
}

.hero-waves {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    z-index: 1;
    display: flex;
    align-items: flex-end;
    pointer-events: none;
}

.easy-typed-cursor {
    margin-left: 5px;
    font-weight: 500;
    animation: blink 1s infinite;
    color: #fff;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

// 背景切换按钮样式
.switch-background-btn {
    position: absolute;
    left: 20px;
    bottom: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 5;

    i {
        color: rgba(255, 255, 255, 0.9);
        font-size: 18px;
        transition: all 0.3s ease;
    }

    &:hover {
        background: rgba(0, 0, 0, 0.5);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        transform: scale(1.05);

        i {
            color: white;
            transform: rotate(180deg);
        }

        .hover-tip {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
    }

    .hover-tip {
        position: absolute;
        left: 50%;
        bottom: 48px;
        transform: translateX(-50%) translateY(10px);
        background: rgba(0, 0, 0, 0.7);
        color: white;
        font-size: 12px;
        padding: 5px 10px;
        border-radius: 5px;
        white-space: nowrap;
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;

        &:after {
            content: '';
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: rgba(0, 0, 0, 0.7) transparent transparent transparent;
        }
    }
}

.arrow-down {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
    animation: bounce 2s infinite;
    z-index: 2;
    color: white;
    font-size: 32px;
    filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.5));
    transition: all 0.3s ease;
}

.arrow-down:hover {
    color: #e6e6ff;
    filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.5));
    transform: translateX(-50%) scale(1.2);
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translate(-50%, 0);
    }

    40% {
        transform: translate(-50%, -20px);
    }

    60% {
        transform: translate(-50%, -10px);
    }
}

@media (max-width: 1024px) {
    .page-title {
        font-size: 3.5rem;
    }

    .subtitle {
        font-size: 1.3rem;
    }

    .arrow-down {
        bottom: 30px;
        font-size: 28px;
        z-index: 5;
    }
}

@media (max-width: 768px) {
    .page-header-container {
        height: 40vh;

        &:not(.is-loading) {
            animation: none;
            background-size: cover;
        }
    }

    .page-title {
        font-size: 3rem;
        margin-bottom: 1rem;
    }

    .subtitle-container,
    .static-subtitle {
        max-width: 95%;
    }

    .subtitle {
        font-size: 1.2rem;
        min-height: 1.8rem;
    }

    .arrow-down {
        bottom: 30px;
        font-size: 28px;
        z-index: 5;
    }
}

@media (max-width: 480px) {
    .page-header-container {
        height: 35vh;
    }

    .page-title {
        font-size: 2.5rem;
        margin-bottom: 0.8rem;
    }

    .subtitle-container,
    .static-subtitle {
        padding: 8px 4px;
    }

    .subtitle,
    .static-subtitle {
        font-size: 1rem;
        min-height: 1.4rem;
    }

    .arrow-down {
        font-size: 24px;
        bottom: 25px;
    }

    .switch-background-btn {
        bottom: 10px;
        left: 10px;
        width: 35px;
        height: 35px;

        i {
            font-size: 16px;
        }
    }
}
</style>